import 'dart:math';

import 'package:flutter/material.dart';

typedef FinishListener = Function(Widget widget);

class LikeAnimalIcon extends StatefulWidget {
  FinishListener mFinishListener;

  LikeAnimalIcon({required this.mFinishListener});

  @override
  _LikeAnimalIconState createState() {
    return _LikeAnimalIconState();
  }
}

class _LikeAnimalIconState extends State<LikeAnimalIcon>
    with TickerProviderStateMixin {
  late AnimationController mAnimationController;

  late Animation<double> opacityAnimation;

  late Animation<double> scaleAnimation;
  late Animation<double> scaleTAnimation;

  late Animation<double> fadeOutAnimation;

  @override
  void initState() {
    super.initState();
    mAnimationController = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 5),
    );
    mAnimationController.addStatusListener((state) {
      if (state.isCompleted) {
        widget.mFinishListener(widget);
      }
    });

    // 透明度动画 (0-0.3时间段)
    opacityAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(
        parent: mAnimationController,
        curve: const Interval(0.0, 0.3, curve: Curves.easeIn),
      ),
    );

    // 缩放动画 (0.3-0.7时间段)
    scaleAnimation = Tween<double>(begin: 100.0, end: 150.0).animate(
      CurvedAnimation(
        parent: mAnimationController,
        curve: const Interval(0.3, 0.7, curve: Curves.elasticOut),
      ),
    );
    scaleTAnimation = Tween<double>(begin: 1, end: 1.5).animate(
      CurvedAnimation(
        parent: mAnimationController,
        curve: const Interval(0.3, 0.7, curve: Curves.elasticOut),
      ),
    );
    // 消失动画 (0.7-1.0时间段)
    fadeOutAnimation = Tween<double>(begin: 1.0, end: 0.0).animate(
      CurvedAnimation(
        parent: mAnimationController,
        curve: const Interval(0.7, 1.0, curve: Curves.easeOut),
      ),
    );
    mAnimationController.forward();
  }

  @override
  void dispose() {
    super.dispose();
    mAnimationController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return _bulid();
  }

  Widget _bulid() {
    // 生成随机角度 (-0.5 到 0.5 弧度，约 -28.6° 到 28.6°)
    final randomAngle = (Random().nextDouble() - 0.5) * 1.0;
    final customAlignment = Alignment.topLeft.add(Alignment(1, 1));
    return AnimatedBuilder(
      animation: mAnimationController,
      builder: (context, child) {
        return Opacity(
          opacity: opacityAnimation.value * fadeOutAnimation.value,
          child: Transform.rotate(
            alignment: Alignment.topLeft,
            angle: randomAngle,
            child: Transform.scale(
              scale: scaleTAnimation.value,
              alignment: Alignment.center,
              child:  ShaderMask(
                shaderCallback: (bounds) {
              /*    return const LinearGradient(
                    colors: [Colors.pinkAccent, Colors.purple],
                    begin: Alignment.topLeft,
                    end: Alignment.bottomRight,
                  ).createShader(bounds);*/
                  return  RadialGradient(
                    colors: [Colors.pinkAccent, Colors.purple],
                    center: Alignment.topLeft.add(Alignment(1, 1)),
                    radius: 0.8,
                  ).createShader(bounds);
                },
                child: const Icon(
                  Icons.favorite,
                  color: Colors.white, // ShaderMask需要白色图标才能正确应用渐变
                  size: 100,
                ),
              ),
            ),
          ),
        );
      },
    );
  }
  Widget _bulid1() {
    return FadeTransition(
      opacity: opacityAnimation,
      child: FadeTransition(
        opacity: fadeOutAnimation,
        child: ScaleTransition(
          scale: scaleTAnimation, // 需要0-1范围的scale值
          child: Icon(
            Icons.favorite,
            color: Colors.blue,
            size: 100, // 固定大小，由ScaleTransition控制缩放
          ),
        ),
      ),
    );
  }

}
